package de.s9mtdev.lol_destiny_home;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;

import javax.imageio.ImageIO;
import javax.swing.JPanel;
import javax.swing.Timer;

import jerklib.Session;
import jerklib.events.IRCEvent;
import jerklib.events.IRCEvent.Type;
import jerklib.listeners.IRCEventListener;

public class RiftView extends JPanel implements MouseListener, IRCEventListener {
	
	Session session;
	String sessionkey;
	Image img;
	ArrayList<Ward> wards = new ArrayList<Ward>();
	Queue<Ward> dump = new ArrayBlockingQueue<Ward>(6);

    public RiftView(InSession is)
    {
    	session = is.getSession();
    	session.addIRCEventListener(this);
    	sessionkey = is.getSessionkey();
       this.addMouseListener(this);
       try {
		img = ImageIO.read(getClass().getResource("map_bmp.bmp"));
	} catch (IOException e) {
		e.printStackTrace();
	}       
	}


    public void paintComponent(Graphics g)
    {   
    	Graphics2D g2d = (Graphics2D)g;
    	g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    	g.setColor(Color.WHITE);
    	g.fillRect(0, 0, 1000, 1000);
        g.drawImage(img, 0, 0, null);
        g.setColor(Color.RED);
    	g.setFont(new Font("Arial", 0, 10));
        long now = System.currentTimeMillis();
        for (Ward w : wards) { 
        	Long timeleft = 180 - ((now - w.when) / 1000);
        	if ( timeleft > 0 ) {
        	    g.setColor(Color.RED);
	        	g.fillOval(w.x - 2, w.y - 2, 8, 8);
	        	
	        	g.setColor(Color.WHITE);
	        	g.drawString(timeleft.toString(), w.x -2, w.y-5);
        	}
        	else dump.add(w);
        }
        wards.remove(dump.poll());
    }

    public void mouseClicked(MouseEvent e)
    {
    	int x = e.getX();
    	int y = e.getY();
    	
    	int x0 = x-8;
    	int x1 = x+8;
    	int y0 = y-8;
    	int y1 = y+8;
    	
    	boolean found = false;
    	
    	for (Ward w : wards) {
    		if (x0 < w.x && x1 > w.x && y0 < w.y && y1 > w.y) {
    			found = true;
    			dump.add(w);
    			sendMsg("unward " + w.x + " " + w.y);
    		}
    	}
    	if (!found) {   	
    		wards.add(new Ward(x, y));
    		sendMsg("ward " + x + " " + y);
    	}
        repaint();
    }

	@Override
	public void mouseEntered(MouseEvent arg0) {}

	@Override
	public void mouseExited(MouseEvent arg0) {}

	@Override
	public void mousePressed(MouseEvent arg0) {}

	@Override
	public void mouseReleased(MouseEvent arg0) {}
	
	class Ward {
		public Ward(int x1, int y1){
			x = x1; y = y1;
			when = System.currentTimeMillis();
		}
		long when = 0;
		int x, y;
	}

	@Override
	public void receiveEvent(IRCEvent e) {
		if ( e.getType() == Type.CHANNEL_MESSAGE) {
			String d = e.getRawEventData();
			int dataStartsAt = d.lastIndexOf(":") +1;
			d = d.substring(dataStartsAt);
			if (d.startsWith("ward")) {
				wards.add(new Ward(Integer.valueOf(d.substring(5,d.lastIndexOf(" "))), Integer.valueOf(d.substring(d.lastIndexOf(" ") + 1))));
			} else if (d.startsWith("unward")) {
				int x = Integer.valueOf(d.substring(7,d.lastIndexOf(" ")));
				int y = Integer.valueOf(d.substring(d.lastIndexOf(" ") + 1));
				for ( Ward ward : wards ) {
					if ( ward.x == x && ward.y == y)
						dump.add(ward);
				}
			}
		}		
	}
	
	private void sendMsg(String a) {
		session.sayChannel(session.getChannel("#dy_"+sessionkey), a);
	}

}
